'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About:
        Information tile widget. Used in the Rig Tools window.
    
    Making Icons:
        1) Icons for Info Tiles should be 60x40px.
        2) Add new images to the Gimp file info_tile.xcf.
            a) Export the image as a PNG.
        3) Add the image to the qt_icons.qrc file.
        4) Run the batch file build_qt_qrc.bat.
            a) This will update qt_icons_resources.py
        5) Loading of the correct asset from the resources file is handled
            automatically by the code below.        
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import sys, types, os, importlib

import maya.OpenMaya as OpenMaya

import masdevallia.settings.PathConstants as PathConstants
import masdevallia.utility.WindowUtility as WinUtility
import masdevallia.meta.RigMeta as RigMeta
import masdevallia.utility.Dag as Dag

from PySide import QtCore
from PySide import QtGui
import masdevallia.windows.icons.qt_icons_resources

'''
============================================================
--->>   GLOBAL VARIABLES
============================================================
'''
form_class, base_class = WinUtility.loadUiType( PathConstants.WIDGET_MODULE_INFO_TILE_GUI )

'''
============================================================
--->>   INFO TILE CLASS
============================================================
''' 
class ModuleInfoTile( form_class, base_class ):
    def __init__( self, module_name=None, module_import_path=None, module_icon=None ):
        super( ModuleInfoTile, self).__init__()
        self.setupUi( self )
        self.setObjectName( module_name )
        
        self.module_name_label.setText( module_name )
        
        self.module_name = module_name
        self.module_import_path = module_import_path
        self.module_icon = module_icon
        
        self.initUI()
        
    def initUI( self ):
        # Create CSS settings for each type of tile.
        main_css = '''
            QFrame {
                background-color: rgb(110,90,90);
                border: 1px solid rgb(96,78,78);
                border-radius: 4px;
            }
            QLabel {
                background-color: transparent;
                border: 10px;
                border-radius:10px;
            }'''
        self.main_frame.setStyleSheet( main_css )
        
        icon_css = 'QFrame {'
        icon_css += 'background-image: url(:/{0}/{1}.png);'.format( 'curve', self.module_icon )
        icon_css += 'background-repeat: no-repeat;'
        icon_css += 'border: 1px solid rgb(96,78,78);'
        icon_css += 'border-radius: 4px;'
        self.pix_frame.setStyleSheet( icon_css )
    
    def mouseDoubleClickEvent( self, event ):
        '''
        Double-click event triggers the creation of the asset associated
        with the tile.
        '''
        # Must always have a RigMeta node before making any modules.
        rig_meta = self.getRigMetaNode( 'RigMeta' )
        
        if rig_meta is None:
            # If there isn't a RigMeta node then make one.
            rig_meta = RigMeta.RigMeta( node_name='RigMeta' )
            
        if rig_meta is not None:
            module = importlib.import_module( self.module_import_path )
            module_class = getattr( module, self.module_name )
            module_instance = module_class( rig_meta=rig_meta, name=self.module_name )
            module_instance.createModule()
            
    def getRigMetaNode( self, name ):
        '''
        Checks if a MetaNode exists.
        
        @param name: String. Name of the MetaNode.
        @return: MetaNode or None
        '''
        if Dag.objExists( name ):
            # Get the MetaNode as a MObject.
            obj = Dag.getMObject( name )
            
            # Get its name attribute value.
            name_plug = Dag.getPlugByName( obj, 'metaName' )
            node_name = Dag.getPlugValue( name_plug )
            
            # Get its type attribute value.
            type_plug = Dag.getPlugByName( obj, 'metaType' )
            node_type = Dag.getPlugValue( type_plug )
            
            # Instance the class object to the passed in name.
            node = RigMeta.RigMeta( node_name=node_name )
            
            return node
        else:
            return None